package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.CustomRentingSeekingListContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.RentSeekingDetail;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsCancelHouseRentSeekingCollection;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsListPage;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsQueryHoseRentSeekingList;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 *
 */
@ActivityScope
public class CustomRentingSeekingListPresenter extends BasePresenter<CustomRentingSeekingListContract.Model, CustomRentingSeekingListContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public CustomRentingSeekingListPresenter(CustomRentingSeekingListContract.Model model, CustomRentingSeekingListContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    /**
     * 获取求租列表
     *
     * @param reqParamsQueryHoseRentSeekingList
     */
    public void getRentSeekingList(ReqParamsQueryHoseRentSeekingList reqParamsQueryHoseRentSeekingList) {
        mModel.getRentSeekingList(AppUtil.entityToMap(reqParamsQueryHoseRentSeekingList)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<RentSeekingDetail>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<RentSeekingDetail>> resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getRentSeekingListSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    /**
     * 删除求租
     *
     * @param seekingId
     */
    public void deleteRentSeeking(String seekingId) {
        mModel.deleteRentSeeking(seekingId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.deleteRentSeekingSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 查询收藏求租列表
     *
     * @param reqParamsListPage
     */
    public void getCollectionSeekingList(ReqParamsListPage reqParamsListPage) {
        mModel.getCollectionSeekingList(AppUtil.entityToMap(reqParamsListPage)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<RentSeekingDetail>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<RentSeekingDetail>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getCollectionSeekingListSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 取消收藏求租
     *
     * @param reqParamsCancelHouseRentSeekingCollection
     */
    public void unCollectionSeeking(ReqParamsCancelHouseRentSeekingCollection reqParamsCancelHouseRentSeekingCollection) {
        mModel.unCollectionSeeking(AppUtil.entityToMap(reqParamsCancelHouseRentSeekingCollection)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.unCollectionSeekingSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}